//
//  DiseasesFoodController.m
//  Food
//
//  Created by dllo on 15/7/19.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DiseasesFoodController.h"
#import "BaseHeader.h"
#import "DiseasesCell.h"
#import "AFNetworkHandler.h"
#import "DiseaseFoodCases.h"
#import "GetHeight.h"
#import "MJRefresh.h"
#import "HomePage_display_Controller.h"
#import "Boutique_class_details_cell.h"
static NSInteger p = 1;

@interface DiseasesFoodController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
// 保存diseaseFood model
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UILabel *diseaseDescribe;
// 提示框 底层灰色
@property (nonatomic, retain) UIView *viewPrompt;
// 提示框 上层灰色
@property (nonatomic, retain) UIView *viewPromptTop;
// 提示框中的scrollView
@property (nonatomic, retain) UIScrollView *scrollPrompt;
// 提示框中的desease (title)
@property (nonatomic, retain) UILabel *lablePrompt;
// 提示框分割线
@property (nonatomic, retain) UIView *lineView;
// 关闭按钮
@property (nonatomic, retain) UIImageView *shutView;
// 病例简介 高度
@property (nonatomic, assign) CGFloat heightDiseaseDescribe;
// 饮食保健 高度
@property (nonatomic, assign) CGFloat heightFitEat;
// 生活保健 高度
@property (nonatomic, assign) CGFloat heightLifeSuit;
@end

@implementation DiseasesFoodController

- (void)dealloc
{
    [_collectionView release];
    [_diseaseModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"naviBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:self.view.bounds];
    img.image = [UIImage imageNamed:@"backGround.jpg"];

    [self.view addSubview:img];
    [self createDetailView];
    [self createCollectionView];
    [self createViewPrompt];
    [self getData];
//    [self performSelector:@selector(getData) withObject:self afterDelay:1];
    [self refreshUp];
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark - 获得数据

- (void) getData
{
    
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/vegetable/tbldisease!getVegetable.do?diseaseId=%@&page=%ld&pageRecord=10&phonetype=0&is_traditional=0", self.diseaseModel.diseaseId, (long)p];
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        NSArray *temp = [result objectForKey:@"data"];
        NSArray *tempArr = [DiseaseFoodCases arrayWithModelByArray:temp];
        if (tempArr.count == 0) {
            self.collectionView.footerRefreshingText = @"数据加载完成";
        } else {
            p++;
        }
        for (DiseaseFoodCases *model in tempArr) {
            [self.arr addObject:model];
        }
        
        [self.collectionView reloadData];
        [activityIndicator stopAnimating];
    }];
}


#pragma mark - 上拉加载
- (void) refreshUp
{
    self.arr = [NSMutableArray array];
   [self.collectionView addFooterWithCallback:^{
       [self getData];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [self.collectionView footerEndRefreshing];
       });
   }];
}


#pragma mark - 详情view
- (void) createDetailView
{
    UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, ScreenWidth - 10 , 100)];
//    detailView.backgroundColor = [UIColor redColor];
    
    detailView.layer.cornerRadius = 10;
    
    detailView.backgroundColor = [UIColor whiteColor];
    
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(detailView.frame.size.width - 40, 0, 40, detailView.frame.size.height)] autorelease];
    
    l.backgroundColor = [UIColor orangeColor];
    
    l.numberOfLines = 2;
    
    l.text = @" 详\n 情";
    
    l.font = [UIFont systemFontOfSize:25];
    
    [detailView setClipsToBounds:YES];
    
    l.textColor = [UIColor whiteColor];
    
    [detailView addSubview:l];
    // show 病症简介
    UILabel *describe = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 30)]autorelease];
    describe.text = @"病症简介";
    describe.textAlignment = NSTextAlignmentLeft;
    [detailView addSubview:describe];
    self.diseaseDescribe =[[[UILabel alloc] initWithFrame:CGRectMake(105, 5, detailView.frame.size.width - 50 - 100, detailView.frame.size.height - 10)] autorelease];
    
//    self.diseaseDescribe.backgroundColor = [UIColor greenColor];
    self.diseaseDescribe.numberOfLines = 0;
    self.diseaseDescribe.text = self.diseaseModel.diseaseDescribe;
    
    [detailView addSubview:self.diseaseDescribe];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detail)];
    
    [detailView addGestureRecognizer:tap];
    
    [self.view addSubview:detailView];
}

#pragma mark - 提示信息中每个提示信息的高度
- (void) getHeight
{
    self.heightDiseaseDescribe = [GetHeight hight:self.diseaseModel.diseaseDescribe wide:self.scrollPrompt.frame.size.width - 50];
    self.heightFitEat = [GetHeight hight:[self.diseaseModel fitEat] wide:self.scrollPrompt.frame.size.width - 50];
    self.heightLifeSuit = [GetHeight hight:[self.diseaseModel lifeSuit] wide:self.scrollPrompt.frame.size.width - 50];
}


#pragma mark - 提示框
-(void) createViewPrompt
{
    
    self.viewPrompt = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)] autorelease];
    
    self.viewPrompt.backgroundColor = [UIColor blackColor];
    
    self.viewPrompt.alpha = 0.9;
    
    self.viewPrompt.hidden = YES;
    
    [self.viewPrompt setClipsToBounds:YES];

    self.lablePrompt = [[[UILabel alloc] initWithFrame:CGRectMake(10, 14, ScreenWidth - 50, 59)] autorelease];
    
    self.lablePrompt.text = [self.diseaseModel diseaseName];
    
    self.lablePrompt.textAlignment = NSTextAlignmentCenter;
    
    self.lablePrompt.font = [UIFont systemFontOfSize:20];
    
    self.lablePrompt.textColor = [UIColor whiteColor];
    
    self.lablePrompt.backgroundColor = [UIColor clearColor];
    
    self.lablePrompt.hidden = YES;
    // 分割线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(10, self.lablePrompt.frame.origin.y + self.lablePrompt.frame.size.height, ScreenWidth - 20, 1)];
    self.lineView.hidden = YES;
    self.lineView.backgroundColor = [UIColor whiteColor];
    
    self.viewPromptTop = [[[UIView alloc] initWithFrame:CGRectMake(10, 15, ScreenWidth - 20, ScreenHeight - 74 - 49 - 70)] autorelease];
    self.viewPromptTop.hidden = YES;
    self.viewPromptTop.backgroundColor = [UIColor grayColor];
    self.viewPromptTop.layer.cornerRadius = 20;
    self.viewPromptTop.alpha = 0.5;
    
#pragma mark - 提示信息
    self.scrollPrompt = [[[UIScrollView alloc] initWithFrame:CGRectMake(15, 15 + self.lablePrompt.frame.size.height, ScreenWidth - 30 , self.viewPromptTop.frame.size.height - self.lablePrompt.frame.size.height)] autorelease];
    [self getHeight];
    self.scrollPrompt.bounces = YES;
//    self.scrollPrompt.contentSize = CGSizeMake(0, self.heightDiseaseDescribe + self.heightFitEat + self.heightLifeSuit);
    self.scrollPrompt.contentSize = CGSizeMake(0, self.heightDiseaseDescribe + self.heightFitEat + self.heightLifeSuit + 35 * 3 + 10);
    self.scrollPrompt.delegate = self;
    self.scrollPrompt.showsHorizontalScrollIndicator = NO;
    self.scrollPrompt.showsVerticalScrollIndicator = NO;
    self.scrollPrompt.contentInset = UIEdgeInsetsMake(10, 5, 0, 5);
    self.scrollPrompt.hidden = YES;

    UILabel *diseaseDescribeTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.scrollPrompt.frame.size.width, 30)] autorelease];
    diseaseDescribeTitle.backgroundColor = [UIColor clearColor];
    diseaseDescribeTitle.textColor = [UIColor whiteColor];
    diseaseDescribeTitle.text = @"病例简介";
    diseaseDescribeTitle.font = [UIFont systemFontOfSize:18];
    [self.scrollPrompt addSubview:diseaseDescribeTitle];
    
    UILabel *diseaseDescribe = [[[UILabel alloc] initWithFrame:CGRectMake(45, 35, self.scrollPrompt.frame.size.width - 50, self.heightDiseaseDescribe)] autorelease];
    diseaseDescribe.numberOfLines = 0;
    diseaseDescribe.textColor = [UIColor whiteColor];
    diseaseDescribe.text = self.diseaseModel.diseaseDescribe;
    [self.scrollPrompt addSubview:diseaseDescribe];
    
    UILabel *fitEatTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, diseaseDescribe.frame.origin.y + diseaseDescribe.frame.size.height, diseaseDescribeTitle.frame.size.width, diseaseDescribeTitle.frame.size.height)] autorelease];
    fitEatTitle.text = @"饮食保健";
    fitEatTitle.backgroundColor = [UIColor clearColor];
    fitEatTitle.font = [UIFont systemFontOfSize:18];
    fitEatTitle.textColor = [UIColor whiteColor];
    [self.scrollPrompt addSubview:fitEatTitle];
    
    UILabel *fitEat = [[[UILabel alloc] initWithFrame:CGRectMake(diseaseDescribe.frame.origin.x, fitEatTitle.frame.origin.y + fitEatTitle.frame.size.height + 5, diseaseDescribe.frame.size.width, self.heightFitEat)] autorelease];
    fitEat.textColor = [UIColor whiteColor];
    fitEat.numberOfLines = 0;
    fitEat.text = [self.diseaseModel fitEat];
    [self.scrollPrompt addSubview:fitEat];
    
    UILabel *lifeSuitTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, fitEat.frame.origin.y + fitEat.frame.size.height, fitEatTitle.frame.size.width, fitEatTitle.frame.size.height)] autorelease];
    lifeSuitTitle.text = @"生活保健";
    lifeSuitTitle.font = [UIFont systemFontOfSize:18];
    lifeSuitTitle.textColor = [UIColor whiteColor];
    [self.scrollPrompt addSubview:lifeSuitTitle];
    
    UILabel *lifeSuit = [[[UILabel alloc] initWithFrame:CGRectMake(diseaseDescribe.frame.origin.x, lifeSuitTitle.frame.origin.y + lifeSuitTitle.frame.size.height + 5, diseaseDescribe.frame.size.width, self.heightLifeSuit)] autorelease];
    lifeSuit.textColor = [UIColor whiteColor];
    lifeSuit.numberOfLines = 0;
    lifeSuit.text = [self.diseaseModel lifeSuit];
    
// 关闭按钮
    
    self.shutView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 50, self.lablePrompt.frame.origin.y + 20, 20, 20)];
    self.shutView.image = [UIImage imageNamed:@"guanbi"];
    self.shutView.hidden = YES;
    [self.shutView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tapShut = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapShut)] autorelease];
    [self.shutView addGestureRecognizer:tapShut];
    [self.scrollPrompt addSubview:lifeSuit];
    [self.view addSubview:self.viewPrompt];
     [self.view addSubview:self.viewPromptTop];
    [self.view addSubview:self.lablePrompt];
    [self.view addSubview:self.scrollPrompt];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.shutView];
    NSLog(@"宽度%f", self.scrollPrompt.frame.size.width - 50);
    
}

// 关闭按钮方法
- (void) tapShut
{
    self.viewPrompt.hidden = YES;
    self.viewPromptTop.hidden = YES;
    self.lablePrompt.hidden = YES;
    self.scrollPrompt.hidden = YES;
    self.lineView.hidden = YES;
    self.shutView.hidden = YES;
}
#pragma mark - 单击详情
- (void) detail
{
    [UIView animateWithDuration:3 animations:^{
        self.viewPrompt.hidden = NO;
        self.viewPromptTop.hidden = NO;
        self.lablePrompt.hidden = NO;
        self.scrollPrompt.hidden = NO;
        self.lineView.hidden = NO;
        self.shutView.hidden = NO;
    }];
    
    
    NSLog(@"sadf");
}

#pragma mark - 菜例推荐collectionView;

- (void) createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 40) / 2, 120 / 375.0 * ScreenWidth);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(5, 115, ScreenWidth - 10, ScreenHeight - 115 - 64 - 49) collectionViewLayout:layout] autorelease];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[DiseasesCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.view addSubview:self.collectionView];
    
    
}

#pragma mark - 协议方法

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiseasesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.foodCases = self.arr[indexPath.item];
    return cell;
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePage_display_Controller *display = [[HomePage_display_Controller alloc] init];
    DiseaseFoodCases *model = self.arr[indexPath.item];
    display.classid = model.vegetableId;
    display.classTitle = model.name;
    display.MVBackGroundimage = model.imagePathThumbnails;
    [self.navigationController pushViewController:display animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidDisappear:(BOOL)animated
{
    p = 1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
